import 'reflect-metadata';
/* import { plainToInstance } from 'class-transformer';
class User{
  id: number;
  firstName: string;
  lastName: string;
  age: number;

  getFullName() {
    return this.firstName + " " + this.lastName;
  }

  isAdult() { 
    return this.age >= 18 ? "成年人" : "未成年人";
  }
}

interface Result<T>{
  code: number;
  data: T[];
  msg: string;
}

// fetch如果想在nodejs中使用，需要nodejs版本至少是18
fetch("https://mock.mengxuegu.com/mock/65b1f3d1c4cd67421b34cd0c/mock_ts/list")
  .then(res => res.json())
  .then((res:Result<User>) => { 
    console.log(res.code);
    console.log(res.msg);

    const users = res.data; // 现在users里面的内容其实之有简单后端的json数据

    // 转换成前端User
    const us = plainToInstance(User, users);


    for (const u of us) {
      console.log(u.id + " " + u.firstName);
      console.log(u.id + " " + u.getFullName() + " " + u.isAdult());
    }
  }) */


// class validator

import {IsNotEmpty,Length,Min,Max, IsPhoneNumber,validate} from "class-validator"

class User { 
  @IsNotEmpty({message:"账号不能为空"})
  @Length(3, 5, {message:"账号必须是3-5个字符长度"})
  loginId: string;

  @Min(9,{message:"年龄至少应该大于10岁"})
  @Max(45,{message:"年龄不能大于45岁"})
  age: number;

  @IsPhoneNumber("CN")
  tel: string;
}

const u = new User();
u.loginId = "jack";
u.age = 9;
u.tel = "23333333333";

validate(u).then(errors => { 
  console.log(errors);
})